<script lang="ts" setup>
import HyForm from '@/base-ui/form';
import { ref } from 'vue';

const props = defineProps({
  searchFormConfig: {
    type: Object,
    required: true,
    default: () => ({})
  }
});

// 1.优化一: formData中的属性应该动态来决定
const formItems = props.searchFormConfig?.formItems ?? [];
const formOriginData: any = {};
for (const item of formItems) {
  formOriginData[item.field] = '';
}
const formData = ref(formOriginData);

const emits = defineEmits(['resetBtnClick', 'queryBtnClick']);
// 重置
const handleResetClick = (): any => {
  for (const key in formOriginData) {
    formData.value[`${key}`] = formOriginData[key];
  }
  emits('resetBtnClick');
};
// 搜索
const handleQueryClick = (): any => {
  emits('queryBtnClick', formData.value);
};
</script>
<template>
  <div class="page-search">
    <hy-form v-bind="searchFormConfig" v-model="formData">
      <template #header>
        <h1 class="header">高级检索</h1>
      </template>
      <template #footer>
        <div class="handle-btns">
          <el-button @click="handleResetClick">重置</el-button>
          <el-button type="primary" @click="handleQueryClick">搜索</el-button>
        </div>
      </template>
    </hy-form>
  </div>
</template>
<style lang="less" scoped>
.page-search {
  background-color: #fff;
}
.header {
  color: red;
}
.handle-btns {
  text-align: right;
  padding: 0 50px 20px 0;
}
</style>
